/**
 *  Copyright 2009-2010 Steven Ottenhoff (Dual-IT)
 * 
 *  This file is part of Clazzified.
 *
 *  Clazzified is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Clazzified is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Clazzified.  If not, see http://www.gnu.org/licenses/gpl.
 * 
 */
package nl.dualit.clazzified.core;

import java.util.HashMap;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;
import java.util.Properties;

import nl.dualit.clazzified.converter.clazz.ClazzConverterFactory;
import nl.dualit.clazzified.core.model.AbstractBuilder;
import nl.dualit.clazzified.core.model.ModelTypes;
import nl.dualit.clazzified.core.model.attribute.IAttribute;
import nl.dualit.clazzified.core.model.clazz.Clazz;
import nl.dualit.clazzified.core.model.clazz.ClazzBuilder;
import nl.dualit.clazzified.store.ClazzData;
import nl.dualit.clazzified.store.StoreConfiguration;
import nl.dualit.clazzified.store.clazz.ClazzFields;
import nl.dualit.clazzified.store.clazz.ClazzStore;

import org.apache.log4j.Logger;

/**
 * The ClazzManager is controlling the clazzes by 
 * providing exclusive access to creation and retrieval of the persistent clazzes.
 * Once a clazz is created or retrieved from the store, the ModelManager observers
 * the clazz throughout its lifetime synchronizing any changes made on the clazz.
 * 
 * @author Steven Ottenhoff
 *
 */
public class ClazzManager implements Observer {
	
	private ClazzStore clazzStore;
	private Logger log = Logger.getLogger( ClazzManager.class );
	private Map<ModelTypes, AbstractBuilder> builders;
	
	/**
	 * Create a new instance of ClazzManager with the properties
	 * read from the default property file.
	 * 
	 */
	public ClazzManager() {
		
		StoreConfiguration.setProperties( ConfigurationManager.getProperties() );		
		
		init();
	}
	
	
	/**
	 * Create an instance of ClazzManager with the properties 
	 * read from the provided Property class.
	 * 
	 * @param props
	 */
	public ClazzManager( Properties props ) {
		
		ConfigurationManager.setProperties( props );
		
		init();
	}
	
	private void init(){
		
		clazzStore = ClazzStore.getInstance();
		builders = new HashMap<ModelTypes,AbstractBuilder>();
		
		builders.put( ModelTypes.CLAZZ, new ClazzBuilder(  ) );
		
		ClazzConverterFactory.setBuilders( builders );
		
	}
	
	private ClazzStore getStore(){
		return clazzStore;
	}
	/**
	 * Returns a new persistent Clazz instance.
	 * 
	 * @param clazzName is mandatory
	 * @param clazzDescription is optional
	 * @param parent is optional
	 * 
	 * @return a new IClazz instance
	 */
	public Clazz getNewClazz( String clazzName, String clazzDescription, Clazz parent ) throws NullPointerException, IllegalArgumentException {
		if( clazzName == null )
			throw new NullPointerException( "ClazzName is null" );
		if( clazzName.length() == 0 )
			throw new IllegalArgumentException( "Clazzname is empty" );
		
		Clazz clazz = ( (ClazzBuilder) builders.get( ModelTypes.CLAZZ ) ).getInstance(clazzName, clazzDescription, parent );
		
		clazz.addObserver( this );
		
		return clazz;
	}
	
	/**
	 * Return an existing clazz from the clazzstore based on the provided name.
	 * The name is unique and returns at most one result, or none if name is not found.
	 * 
	 * @param clazzName
	 * 
	 * @return Clazz that matches the provides clazzName
	 */
	public Clazz getClazzByName( String clazzName ){
		if( clazzName == null )
			throw new NullPointerException( "ClazzName is null" );
		if( clazzName.length() == 0 )
			throw new IllegalArgumentException( "Clazzname is empty" );
		
		Map<ClazzFields,String> data = new HashMap<ClazzFields,String>();
		
		data.put( ClazzFields.CLAZZ_NAME, clazzName );
		
		
		Clazz clazz =  ( ( ClazzBuilder) builders.get( ModelTypes.CLAZZ ) ).getInstance(
				getStore().getClazz( clazzName ) );
		
		if( clazz == null )
			throw new NullPointerException("Recieved null clazz from builder" );
		
		clazz.addObserver( this );
		
		return clazz;
	}
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	public Clazz getClazzById( Long id ){
		
		if( id == null )
			throw new NullPointerException( "Clazz id is null." );
		if( id <= 0 )
			throw new IllegalArgumentException( "Negative or zero id not allowed, recieved " + id );
		
		Clazz clazz =  ( ( ClazzBuilder) builders.get( ModelTypes.CLAZZ ) ).getInstance(
				getStore().getClazz( id ) );
		
		if( clazz == null )
			throw new NullPointerException("Recieved null clazz from builder" );
		
		clazz.addObserver( this );
		
		return clazz;
	}
	
	public void deletetClazz( Clazz clazz ){
		if( clazz == null )
			throw new NullPointerException( "Clazz is null" );
		if( clazz.getId() == null )
			throw new NullPointerException( "Clazz id is null" );
		
		clazzStore.deleteClazz( clazz );
		
		clazz.setId( null );
	}
	
	/**
	 * Observes the clazz for changes and requests the store
	 * to synchronize the changes.
	 * 
	 */
	@Override
	public void update(Observable o, Object arg) {
		
		log.debug( "Recieved request for update" );
		
		if( o instanceof Clazz )
			clazzStore.updateClazz( ( (Clazz)o ) );
		
		else if( o instanceof IAttribute )
			clazzStore.updateAttribute( ( (IAttribute)o ) );
		// TODO Handle updates from the rest
		else
			log.warn( "Ignoring update event from unknow class " + o.getClass() );
		
		log.debug( "Done with request for update" );
	}
	
}
